package com.fyl.basic_algorithm.sorts;

import java.util.Arrays;

/**
 * @author 冯永龙
 * @date 2021/3/15 16:42
 * 冒泡、选择、插入、归并、
 */
public class Sorts {

    //快速排序
    public static void quickSort(int[] arr, int left, int right) {
        if (left >= right) return;

        int q = partition(arr, left, right);
        quickSort(arr, left, q - 1);
        quickSort(arr, q + 1, right);
    }

    //分区函数
    public static int partition(int[] arr, int left, int right) {
        int pivot = arr[right];
        int i = left;
        for (int j = left; j < right; j++) {
            if (arr[j] < pivot) {
                if (i == j) {
                    ++i;
                }else {
                    int temp = arr[i];
                    arr[i++] = arr[j];
                    arr[j] = temp;
                }
            }
        }

        int tmp = arr[i];
        arr[i] = arr[right];
        arr[right] = tmp;

        return i;
    }

    /**
     * 归并排序
     *
     * @param arr   传入数组
     * @param left  数组起始下标
     * @param right 数组末尾下标
     */
    public static void mergeSort(int[] arr, int left, int right) {
        if (left >= right) return;
        int q = (left + right) / 2;
        mergeSort(arr, left, q);
        mergeSort(arr, q + 1, right);
        merge_02(arr, left, q, right);

    }

    //归并排序的合并函数带哨兵
    public static void merge(int[] arr, int left, int q, int right) {
        //左右数组多一个位置留作哨兵
        int[] leftArr = new int[q - left + 2];
        int[] rightArr = new int[right - q + 1];

        //将左边数组元素填充
        for (int i = 0; i <= q - left; i++) {
            leftArr[i] = arr[left + i];
        }
        //添加第一个数组哨兵（最大值）
        leftArr[q - left + 1] = Integer.MAX_VALUE;

        //将右边数组元素填充
        for (int i = 0; i < right - q; i++) {
            rightArr[i] = arr[q + 1 + i];
        }
        //第二个数组添加哨兵
        rightArr[right - q] = Integer.MAX_VALUE;

        int i = 0;
        int j = 0;
        int k = left;
        while (k <= right) {
            //当左边数组达到哨兵时，i不在增加，知道右边数组读取完剩余值，同理右边数组也一样
            if (leftArr[i] <= rightArr[j]) {
                arr[k++] = leftArr[i++];
            } else {
                arr[k++] = rightArr[j++];
            }
        }
    }

    //归并排序的合并函数不带哨兵
    public static void merge_02(int[] arr, int left, int q, int right) {
        int i = left;
        int j = q + 1;
        int k = 0;
        int[] temp = new int[right - left + 1];
        while (i <= q && j <= right) {
            if (arr[i] <= arr[j]) {
                temp[k++] = arr[i++];
            } else {
                temp[k++] = arr[j++];
            }
        }

        //如果左边或右边分区的数组还未放入temp中则将其放入
        int start = i;
        int end = q;
        if (j <= right) {
            start = j;
            end = right;
        }

        while (start <= end) {
            temp[k++] = arr[start++];
        }

        //将排序好的temp数组copy到arr数组
        for (int l = 0; l <= right - left; l++) {
            arr[l + left] = temp[l];
        }
    }

    // 冒泡排序，a是数组，n表示数组大小
    public static void bubbleSort(int[] a, int n) {
        if (n <= 1) return;

        for (int i = 0; i < n; i++) {
            // 提前退出标记
            boolean flag = false;
            for (int j = 0; j < n - i - 1; j++) {
                if (a[j] > a[j + 1]) {  //交换
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                    flag = true;
                }
            }
            if (!flag) break;//没有交换提前退出
        }
    }

    // 插入排序，a表示数组，n表示数组大小
    public static void insertionSort(int[] a, int n) {
        if (n <= 1) return;

        for (int i = 1; i < n; i++) {
            int value = a[i];
            int j = i - 1;
            //查找要插入的位置并移动数据
            for (; j >= 0; --j) {
                if (a[j] > value) {
                    a[j + 1] = a[j];
                } else {
                    break;
                }
            }
            a[j + 1] = value;
        }
    }

    // 选择排序，a表示数组，n表示数组大小
    public static void selectionSort(int a[], int n) {
        if (n <= 1) return;

        for (int i = 0; i < n - 1; i++) {
            //查找最小值
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (a[j] < a[minIndex]) {
                    minIndex = j;
                }
            }

            //交换
            int temp = a[i];
            a[i] = a[minIndex];
            a[minIndex] = temp;
        }
    }

    public static void main(String[] args) {
        int[] array = new int[]{3, 5, 2, 1, 4, 9, 9, 7, 8};
        quickSort(array,0,array.length-1);
//        mergeSort(array, 0, array.length - 1);
//        bubbleSort(array, array.length);
        System.out.println(Arrays.toString(array));
    }
}
